#coding: utf8
import syslog
import sys
import traceback


class Logger(object):

    def __init__(self, id):
        self.id = id
        self.prepare()
    
    def prepare(self):
        pass

    def debug(self, msg):
        self._log(self.LOG_DEBUG, msg)

    def info(self, msg):
        self._log(self.LOG_INFO, msg)
    
    def notice(self, msg):
        self._log(self.LOG_NOTICE, msg)
    
    def warn(self, msg):
        self._log(self.LOG_WARNING, msg)
    
    def error(self, msg):
        self._log(self.LOG_ERR, msg)
    
    def exception(self, ex):
        self.error(self.__exception_msg(ex))
    
    def __exception_msg(self, ex):
        t, v, tb = sys.exc_info()
        return '%s,%s,%s,%s' % (traceback.format_tb(tb), t, v, ex)


class SysLogger(Logger):
    
    facility = syslog.LOG_LOCAL6
    LOG_DEBUG = syslog.LOG_DEBUG
    LOG_INFO = syslog.LOG_INFO
    LOG_NOTICE = syslog.LOG_NOTICE
    LOG_WARNING = syslog.LOG_WARNING
    LOG_ERR = syslog.LOG_ERR
    
    def prepare(self):
        syslog.openlog(self.id, syslog.LOG_NDELAY, self.facility)
    
    def _log(self, cate, msg):
        syslog.syslog(cate, '%s' % msg)


class StdLogger(Logger):
    
    facility = syslog.LOG_LOCAL6
    LOG_DEBUG = "Debug"
    LOG_INFO = "Info"
    LOG_NOTICE = "Notice"
    LOG_WARNING = "Warning"
    LOG_ERR = "Error"
    
    def _log(self, cate, msg):
        print ('%s: %s|%s' % (cate, self.id, msg))


logger = Logger('ProjectName')

